import { Injectable } from '@angular/core';

export interface PageInterface {
  pageIndex: Number
  total: Number
  pageSize: Number
  loading: Boolean,
  totalPage: Number
}

@Injectable()
export class PageService implements PageInterface {
  public pageIndex: number;
  public total: number;
  public pageSize: number;
  public loading: boolean;
  public totalPage: number

  constructor() {
    this.pageIndex = 1
    this.total = 0
    this.pageSize = 10
    this.loading = false
  }

  /**
   * 获取分页配置信息
   */
  getConfig() {
    return {
      page: this.pageIndex - 1,
      size: this.pageSize
    }
  }

  /**
   * 更新分页配置信息
   * @param param
   */
  update(count) {
    count.then(result => {
      this.total = result
      this.totalPage =  Math.ceil(result / this.pageSize)
    })
  }
}
